package com.guat.servlet.article;

import com.google.gson.Gson;
import com.guat.service.ArticleService;
import com.guat.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

@WebServlet("/updateArticle")
public class UpdateServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置前端返回数据使用UTF-8编码,防止乱码
        req.setCharacterEncoding("UTF-8");
        //获取前端传递的数据
        String id = req.getParameter("id");
        String title = req.getParameter("title");
        String content = req.getParameter("content");

        Map<String, Object> map = new ArticleService().updateArticle(id, title, content);
        //设置返回前端数据使用UTF-8编码,防止乱码
        resp.setCharacterEncoding("UTF-8");
        //设置响应内容为json,以便前端正确解析返回的数据
        resp.setContentType("application/json");
        //将Map集合转换为json
        Gson gson = new Gson();
        String mapJson = gson.toJson(map);
        //将数据写入输入流中
        resp.getWriter().write(mapJson);
    }
}
